//
//  ShowDetailViewController.m
//  Mahjong
//
//  Created by KING on 16/10/19.
//  Copyright © 2016年 swift. All rights reserved.
//

#import "ShowDetailViewController.h"
#import "GoodsListTableViewCell.h"
#import "GoodsListCollectionViewCell.h"
#import "SelectView.h"
#import "SynthesizeView.h"
#import "GoodsDetailViewController.h"
#import "AppTools.h"
#import "ListShowModel.h"
#define kSelectViewHeight 40

static NSString *identifierForGoodsListTableViewCell = @"GoodsListTableViewCell";
static NSString *identifierForGoodsListCollectionViewCell = @"GoodsListCollectionViewCell";
@interface ShowDetailViewController ()
<SelectViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource,UITableViewDelegate,UITableViewDataSource,SynthesizeViewDelegate>

/** ContentTableView */
@property (nonatomic , strong) UIView *contentTableView;

/** ContentCollectionView */
@property (nonatomic , strong) UIView *contentCollectionView;

/** tableView */
@property (nonatomic , strong) UITableView *tableView;

/** collectionView */
@property (nonatomic , strong) UICollectionView *collectionView;

/** selectView */
@property (nonatomic , strong) SelectView *selectView;

/** dataArray */
@property (nonatomic , strong) NSArray *dataArray;
@property (nonatomic, strong) NSMutableArray *dateArray;
@property (nonatomic, strong) UIView *titleView;

@end

@implementation ShowDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationController.navigationBar.translucent = NO;
    [self createSelectView];
    [self createCollectionView];
    [self createTableView];
    [self dataHandle];
    [self naVC];
    [self create];
    [self createTitleView];
    
}
-(void)createTitleView{
    self.titleView = [[UIView alloc] initWithFrame:CGRectMake(0, -1000, SCREEN_WIDTH, SCREENH_HEIGHT)];
    self.titleView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.titleView];
    
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREENH_HEIGHT)];
    backView.backgroundColor = [UIColor grayColor];
    backView.alpha = 0.3;
    [self.titleView addSubview:backView];
    
    UIView *back = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
    back.backgroundColor = [UIColor whiteColor];
    [self.titleView addSubview:back];
    
    UIButton *zonghe = [UIButton buttonWithType:UIButtonTypeCustom];
    zonghe.frame = CGRectMake(10, 10, 100, 30);
    [zonghe setTitle:@"综合排序" forState:UIControlStateNormal];
    [zonghe setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.titleView addSubview:zonghe];
    [zonghe addTarget:self action:@selector(zongAction) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *xinpin = [UIButton buttonWithType:UIButtonTypeCustom];
    xinpin.frame = CGRectMake(10, 50, 100, 30);
    [xinpin setTitle:@"新品优先" forState:UIControlStateNormal];
    [xinpin setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.titleView addSubview:xinpin];
    [zonghe addTarget:self action:@selector(xinAction) forControlEvents:UIControlEventTouchUpInside];
    
    
    UIButton *jiage = [UIButton buttonWithType:UIButtonTypeCustom];
    jiage.frame = CGRectMake(10, 90, 100, 30);
    [jiage setTitle:@"价格区间" forState:UIControlStateNormal];
    [jiage setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.titleView addSubview:jiage];
    
    UITextField *min = [[UITextField alloc] initWithFrame:CGRectMake((20*SCREEN_WIDTH)/375, 135, (90*SCREEN_WIDTH)/375, 30)];
    //    min.placeholder = @"最低价";
    min.backgroundColor =[UIColor colorWithRed:239/255.0 green:239/255.0 blue:239/255.0 alpha:1];
    [self.titleView addSubview:min];
    UITextField *max = [[UITextField alloc] initWithFrame:CGRectMake((120*SCREEN_WIDTH)/375, 135, (90*SCREEN_WIDTH)/375, 30)];
    //    max.placeholder = @"最高价";
    max.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:239/255.0 alpha:1];
    [self.titleView addSubview:max];
    
    UIButton *sear = [UIButton buttonWithType:UIButtonTypeCustom];
    sear.frame = CGRectMake((230*SCREEN_WIDTH)/375, 135, (50*SCREEN_WIDTH)/375, 30);
    sear.backgroundColor = [UIColor redColor];
    sear.layer.cornerRadius = 5;
    [sear setTitle:@"搜索" forState:UIControlStateNormal];
    [sear setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.titleView addSubview:sear];
    
}
-(void)zonghe:(NSInteger)num{
    if (num == 1) {
        self.titleView.frame = CGRectMake(0, 40, SCREEN_WIDTH, SCREENH_HEIGHT);
        
    }
    if (num == 0) {
        self.titleView.frame = CGRectMake(0, -1000, SCREEN_WIDTH, SCREENH_HEIGHT);
        
    }
    
//    [self create:nil];
}
-(void)jiage{
//    [self create:@"moneys"];
}
-(void)xiaoliang{
//    [self create:@"salecount"];
}
-(void)zongAction{
//    [self create:nil];
}
-(void)xinAction{
//    [self create:@"giftaddtime"];
}

-(void)create{
    self.dateArray = [NSMutableArray array];
    [AppTools createAFN:[NSString stringWithFormat:@"http://api03.m0571.com/WebJson.aspx?book=GetPromotionList&page=1&PromotionValue=%@",self.value] success:^(id result) {
        NSLog(@"%@",[result[0] objectForKey:@"date"]);
        for (NSDictionary *dic in [result[0] objectForKey:@"date"]) {
            ListShowModel *model = [[ListShowModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dateArray addObject:model];
        }
        [self.tableView reloadData];
        [self.collectionView reloadData];
    } error:^(NSError *error) {
        
        
    }];
}
-(void)naVC{
    self.navigationItem.title = @"商品列表";
    UIColor * color = [UIColor blackColor];
    NSDictionary * dict = [NSDictionary dictionaryWithObject:color forKey:NSForegroundColorAttributeName];
    self.navigationController.navigationBar.titleTextAttributes = dict;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"nav_but_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftAction:)];
    //    [self createData];
    
}
-(void)leftAction:(UIBarButtonItem *)btn{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)dataHandle{
    self.dataArray ;
}

- (void)createSelectView{
    self.selectView = [[SelectView alloc] initSelectViewWithFrame:CGRectMake(0, 0, kUIScreenWidth, kSelectViewHeight)];
    self.selectView.delegate = self;
    [self.view addSubview:self.selectView];
}

- (void)createCollectionView{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.sectionInset = UIEdgeInsetsMake(10, 0, 10, 0);
    layout.minimumLineSpacing = 5;
    layout.minimumInteritemSpacing = 5;
    CGFloat width = (kUIScreenWidth - 5)/2.0;
    CGFloat height = 200;
    layout.itemSize = CGSizeMake(width, height);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, kSelectViewHeight, kUIScreenWidth, kUIScreenHeight - kNavigationBarHeight - kSelectViewHeight) collectionViewLayout:layout];
    self.collectionView.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:239/255.0 alpha:1];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.showsVerticalScrollIndicator = NO;
    
    [self.collectionView registerNib:[UINib nibWithNibName:identifierForGoodsListCollectionViewCell bundle:nil] forCellWithReuseIdentifier:identifierForGoodsListCollectionViewCell];
    
    [self.view addSubview:self.collectionView];
    
}

- (void)createTableView{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kSelectViewHeight, kUIScreenWidth, kUIScreenHeight - kNavigationBarHeight - kSelectViewHeight) style:(UITableViewStylePlain)];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.rowHeight = 120;
    
    [self.tableView registerNib:[UINib nibWithNibName:identifierForGoodsListTableViewCell bundle:nil] forCellReuseIdentifier:identifierForGoodsListTableViewCell];
    self.tableView.hidden = YES;
    [self.view addSubview:self.tableView];
}

#pragma mark -- UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dateArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    GoodsListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierForGoodsListTableViewCell forIndexPath:indexPath];
    ListShowModel *model = self.dateArray[indexPath.row];
    cell.contentLabel.text = model.Giftname;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    GoodsDetailViewController *goodsDetailVC = [[GoodsDetailViewController alloc] init];
    [self.navigationController pushViewController:goodsDetailVC animated:YES];
}
#pragma mark -- UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dateArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    GoodsListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifierForGoodsListCollectionViewCell forIndexPath:indexPath];
    ListShowModel *model = self.dateArray[indexPath.item];
    cell.contentLabel.text = model.Giftname;
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    GoodsDetailViewController *goodsDetailVC = [[GoodsDetailViewController alloc] init];
    ListShowModel *model = self.dateArray[indexPath.item];
    goodsDetailVC.Id = model.Id;
    goodsDetailVC.shopid = model.shopid;
    [self.navigationController pushViewController:goodsDetailVC animated:YES];
}
#pragma mark -- SelectViewDelegate
- (void)selectView:(SelectView *)selectView changeBtn:(UIButton *)changeBtn{
    self.collectionView.hidden = !self.collectionView.hidden;
    self.tableView.hidden = !self.tableView.hidden;
}

- (void)searchWithMinPrice:(NSInteger)minPrice MaxPrice:(NSInteger)maxPrice ModeName:(NSString *)modeName{
    // 获取到筛选数据 进行网络请求
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    self.navigationController.navigationBar.translucent = NO;
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:YES];
    //    self.navigationController.navigationBar.translucent = NO;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
